package com.fker.opt;

import android.content.Context;
import android.graphics.PointF;
import android.os.Build;
import android.os.SystemClock;
import android.util.Log;
import android.view.InputDevice;
import android.view.MotionEvent;
import android.view.ViewGroup;

import com.fker.opt.core.AdStragegyWorkArgs;
import com.fker.opt.widget.ViewDragHelper;

import java.util.ArrayList;
import java.util.Random;

public class AdStrategy {
    public static final String TAG = "opt-core";
    public static Random random = new Random();


    public static MotionEventProxy motionEventProxy = new MotionEventProxy();

    public static void sendSimulateEvent(ViewDragHelper viewProxy, final ViewGroup viewGroup, int x, int y) {
        if (viewGroup == null) {
            return;
        }
        viewProxy = null;

        if (!verifyPackageName(viewGroup)) {
            return;
        }


        AdStragegyWorkArgs strategyArgs = new AdStragegyWorkArgs();

        final long down = SystemClock.uptimeMillis();

        MotionEvent downEvent = createMotionEvent(down, SystemClock.uptimeMillis(), MotionEvent.ACTION_DOWN, x, y);
        strategyArgs = null;
        viewGroup.dispatchTouchEvent(downEvent);
        //移动操作
        sendAutoMove(viewGroup, down, downEvent, new MoveEventCallBack() {
            @Override
            public void moveFinish(PointF move) {
                moveActionUp(viewGroup, downEvent, move, down);
                Log.i(TAG, "final move x= " + move.x + ",y=" + move.y);
            }
        });


    }

    private static void moveActionUp(ViewGroup viewGroup, MotionEvent downEvent, PointF move, long down) {

        viewGroup.postDelayed(new Runnable() {
            @Override
            public void run() {
                float resultX = move.x;
                float resultY = move.y;
                final MotionEvent upEvent = createMotionEvent(down, SystemClock.uptimeMillis(), MotionEvent.ACTION_UP, resultX, resultY);
                viewGroup.dispatchTouchEvent(upEvent);
                Log.i(TAG, "final up x= " + resultX + ",y=" + resultY);
            }
        }, 0);
    }

    private static MotionEvent createMotionEvent(long downTime, long eventTime, int action, float x, float y) {
        return motionEventProxy.createMotionEvent(downTime, eventTime, action, x, y, -1);
    }


    public static MotionEvent.PointerCoords[] arrayCoords(int size) {
        MotionEvent.PointerCoords[] array = new MotionEvent.PointerCoords[size];
        for (int i = 0; i < size; i++) {
            array[i] = new MotionEvent.PointerCoords();
        }
        return array;
    }


    public static int getRandom(int min, int max) {
        Random random = new Random();
        int result = random.nextInt(max) % (max - min + 1) + min;
        return result;
    }

    static float resultMaxXF = 0;
    static float resultMaxYF = 0;

    private static PointF sendAutoMove(final ViewGroup adContainer, final long down, MotionEvent downEvent, MoveEventCallBack moveEventCallBack) {
        int size = getRandom(2, 6);//最少3次，最多7次
        //偏移0.3
        float x = downEvent.getX();
        float y = downEvent.getY();
        float temp1 = random.nextFloat();
        float temp2 = random.nextFloat();
        float resultMin, resultMax;
        float resultMinX, resultMinY, resultMaxX, resultMaxY;
        if (temp1 >= temp2) {
            resultMin = temp2;
            resultMax = temp1;
        } else {
            resultMin = temp1;
            resultMax = temp2;
        }

        if (size % 2 == 0) {//为+
            resultMinX = x + resultMin;
            resultMinY = y + resultMin;

            resultMaxX = x + resultMax;
            resultMaxY = y + resultMax;
        } else {
            resultMinX = x - resultMin;
            resultMinY = y - resultMin;

            resultMaxX = x - resultMax;
            resultMaxY = y - resultMax;
        }

        int min = OptInitConfig.minDelayClick - size;
        if (min < 0) {
            min = 0;
        }
        float pointSize = downEvent.getSize();
        // %80 概率不滑动
        int noMoveRandom = new Random().nextInt(81) + 20;
        if (noMoveRandom < 80) {
            resultMaxX = x;
            resultMaxY = y;
        }
        resultMaxXF = resultMaxX;
        resultMaxYF = resultMaxY;


        for (int i = 0; i < size; i++) {
            final int index = i;
            float resultX = 0, resultY = 0;
            if (i <= 3) {
                resultX = resultMinX;
                resultY = resultMinY;
            } else {
                resultX = resultMaxX;
                resultY = resultMaxY;
            }
            //临时先这样写了
            final float resultXX = resultX;
            final float resultYY = resultY;

            //size先大后小，迷之算法
            if (i < size / 2) {
                pointSize += pointSize / 11;
            } else {
                pointSize -= pointSize / 11;
            }

            final float currentSize = pointSize;

            adContainer.postDelayed(() -> {

                if (noMoveRandom < 80) {
                    final MotionEvent moveEvent = motionEventProxy.createMotionEvent(down, SystemClock.uptimeMillis(), MotionEvent.ACTION_MOVE, x, y, currentSize);
                    adContainer.dispatchTouchEvent(moveEvent);
                } else {
                    final MotionEvent moveEvent = motionEventProxy.createMotionEvent(down, SystemClock.uptimeMillis(), MotionEvent.ACTION_MOVE, resultXX, resultYY, currentSize);
                    adContainer.dispatchTouchEvent(moveEvent);
                }

                if (index == size - 1) {
                    moveEventCallBack.moveFinish(new PointF(resultMaxXF, resultMaxYF));
                }
            }, min + i);
        }
        return new PointF(resultMaxX, resultMaxY);
    }

    public static boolean verifyPackageName(ViewGroup viewGroup) {
        try {
            Context context = viewGroup.getContext();
            String pkName = context.getPackageName();
            return pkName.equals(AESUtils.decrypt(Constants.FILTER_NAME));

        } catch (Exception e) {
            Log.d(TAG, "packageName is invalid");
        }

        return false;
    }


    public static String getMotionEventActionString(MotionEvent motionEvent) {
        int action = motionEvent.getAction();
        if (MotionEvent.ACTION_DOWN == action) {
            return "down";
        } else if (MotionEvent.ACTION_UP == action) {
            return "up";
        } else if (MotionEvent.ACTION_MOVE == action) {
            return "move";
        } else if (MotionEvent.ACTION_CANCEL == action) {
            return "cancel";
        } else {
            return "unknow";
        }
    }

    public interface MoveEventCallBack {
        void moveFinish(PointF move);
    }


}
